package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import androidx.core.view.ViewCompat;
import java.lang.reflect.Method;
import o.ah.g;
import o.bt;
import o.ch;
import o.iw;

public class ListPopupWindow implements bt {
    private static Method ˊ;
    private static Method ˏ;
    private static Method ᐝ;
    final Handler ʻ;
    private boolean ʻॱ;
    protected PopupWindow ʼ;
    private int ʼॱ;
    private ListAdapter ʽ;
    private boolean ʽॱ;
    private boolean ʾ;
    private DataSetObserver ʿ;
    private View ˈ;
    private final d ˉ;
    private OnItemSelectedListener ˊˊ;
    private OnItemClickListener ˊˋ;
    private int ˊॱ;
    private Drawable ˊᐝ;
    ch ˋ;
    private View ˋˊ;
    private final Rect ˋˋ;
    private int ˋॱ;
    private final a ˋᐝ;
    private Rect ˌ;
    private Runnable ˍ;
    int ˎ;
    private final b ˎˎ;
    private boolean ˏˎ;
    private int ˏॱ;
    private int ͺ;
    final e ॱ;
    private int ॱˊ;
    private boolean ॱˋ;
    private int ॱˎ;
    private Context ॱॱ;
    private boolean ॱᐝ;
    private boolean ᐝॱ;

    class a implements Runnable {
        final /* synthetic */ ListPopupWindow ॱ;

        a(ListPopupWindow listPopupWindow) {
            this.ॱ = listPopupWindow;
        }

        public void run() {
            this.ॱ.ˊॱ();
        }
    }

    class b implements OnScrollListener {
        final /* synthetic */ ListPopupWindow ˏ;

        b(ListPopupWindow listPopupWindow) {
            this.ˏ = listPopupWindow;
        }

        public void onScroll(AbsListView absListView, int i, int i2, int i3) {
        }

        public void onScrollStateChanged(AbsListView absListView, int i) {
            if (i == 1 && !this.ˏ.ͺ() && this.ˏ.ʼ.getContentView() != null) {
                this.ˏ.ʻ.removeCallbacks(this.ˏ.ॱ);
                this.ˏ.ॱ.run();
            }
        }
    }

    class c extends DataSetObserver {
        final /* synthetic */ ListPopupWindow ˎ;

        c(ListPopupWindow listPopupWindow) {
            this.ˎ = listPopupWindow;
        }

        public void onChanged() {
            if (this.ˎ.ˎ()) {
                this.ˎ.ˊ();
            }
        }

        public void onInvalidated() {
            this.ˎ.ˋ();
        }
    }

    class d implements OnTouchListener {
        final /* synthetic */ ListPopupWindow ॱ;

        d(ListPopupWindow listPopupWindow) {
            this.ॱ = listPopupWindow;
        }

        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            if (action == 0 && this.ॱ.ʼ != null && this.ॱ.ʼ.isShowing() && x >= 0 && x < this.ॱ.ʼ.getWidth() && y >= 0 && y < this.ॱ.ʼ.getHeight()) {
                this.ॱ.ʻ.postDelayed(this.ॱ.ॱ, 250);
            } else if (action == 1) {
                this.ॱ.ʻ.removeCallbacks(this.ॱ.ॱ);
            }
            return false;
        }
    }

    class e implements Runnable {
        final /* synthetic */ ListPopupWindow ॱ;

        e(ListPopupWindow listPopupWindow) {
            this.ॱ = listPopupWindow;
        }

        public void run() {
            if (this.ॱ.ˋ != null && ViewCompat.ˋˊ(this.ॱ.ˋ) && this.ॱ.ˋ.getCount() > this.ॱ.ˋ.getChildCount() && this.ॱ.ˋ.getChildCount() <= this.ॱ.ˎ) {
                this.ॱ.ʼ.setInputMethodMode(2);
                this.ॱ.ˊ();
            }
        }
    }

    static {
        try {
            ˏ = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", new Class[]{Boolean.TYPE});
        } catch (NoSuchMethodException e) {
            Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
        }
        try {
            ˊ = PopupWindow.class.getDeclaredMethod("getMaxAvailableHeight", new Class[]{View.class, Integer.TYPE, Boolean.TYPE});
        } catch (NoSuchMethodException e2) {
            Log.i("ListPopupWindow", "Could not find method getMaxAvailableHeight(View, int, boolean) on PopupWindow. Oh well.");
        }
        try {
            ᐝ = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", new Class[]{Rect.class});
        } catch (NoSuchMethodException e3) {
            Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
        }
    }

    public ListPopupWindow(@NonNull Context context) {
        this(context, null, o.ah.c.ˋᐝ);
    }

    public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, o.ah.c.ˋᐝ);
    }

    public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i) {
        this(context, attributeSet, i, 0);
    }

    public ListPopupWindow(@NonNull Context context, @Nullable AttributeSet attributeSet, @AttrRes int i, @StyleRes int i2) {
        this.ͺ = -2;
        this.ˏॱ = -2;
        this.ॱˊ = 1002;
        this.ॱˋ = true;
        this.ॱˎ = 0;
        this.ʽॱ = false;
        this.ʾ = false;
        this.ˎ = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        this.ʼॱ = 0;
        this.ॱ = new e(this);
        this.ˉ = new d(this);
        this.ˎˎ = new b(this);
        this.ˋᐝ = new a(this);
        this.ˋˋ = new Rect();
        this.ॱॱ = context;
        this.ʻ = new Handler(context.getMainLooper());
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, g.ﹳॱ, i, i2);
        this.ˊॱ = obtainStyledAttributes.getDimensionPixelOffset(g.ꜟॱ, 0);
        this.ˋॱ = obtainStyledAttributes.getDimensionPixelOffset(g.ꜞॱ, 0);
        if (this.ˋॱ != 0) {
            this.ᐝॱ = true;
        }
        obtainStyledAttributes.recycle();
        this.ʼ = new AppCompatPopupWindow(context, attributeSet, i, i2);
        this.ʼ.setInputMethodMode(1);
    }

    public void ˏ(@Nullable ListAdapter listAdapter) {
        if (this.ʿ == null) {
            this.ʿ = new c(this);
        } else if (this.ʽ != null) {
            this.ʽ.unregisterDataSetObserver(this.ʿ);
        }
        this.ʽ = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.ʿ);
        }
        if (this.ˋ != null) {
            this.ˋ.setAdapter(this.ʽ);
        }
    }

    public void ˋ(int i) {
        this.ʼॱ = i;
    }

    public void ˎ(boolean z) {
        this.ˏˎ = z;
        this.ʼ.setFocusable(z);
    }

    public boolean ॱॱ() {
        return this.ˏˎ;
    }

    public void ˋ(Drawable drawable) {
        this.ˊᐝ = drawable;
    }

    @Nullable
    public Drawable ʼ() {
        return this.ʼ.getBackground();
    }

    public void ˊ(@Nullable Drawable drawable) {
        this.ʼ.setBackgroundDrawable(drawable);
    }

    public void ˊ(@StyleRes int i) {
        this.ʼ.setAnimationStyle(i);
    }

    @Nullable
    public View ʻ() {
        return this.ˋˊ;
    }

    public void ˏ(@Nullable View view) {
        this.ˋˊ = view;
    }

    public int ʽ() {
        return this.ˊॱ;
    }

    public void ˎ(int i) {
        this.ˊॱ = i;
    }

    public int ˏॱ() {
        if (this.ᐝॱ) {
            return this.ˋॱ;
        }
        return 0;
    }

    public void ˏ(int i) {
        this.ˋॱ = i;
        this.ᐝॱ = true;
    }

    @RestrictTo({androidx.annotation.RestrictTo.d.LIBRARY_GROUP})
    public void ˏ(Rect rect) {
        this.ˌ = rect;
    }

    public void ॱ(int i) {
        this.ॱˎ = i;
    }

    public int ॱˊ() {
        return this.ˏॱ;
    }

    public void ॱॱ(int i) {
        this.ˏॱ = i;
    }

    public void ʼ(int i) {
        Drawable background = this.ʼ.getBackground();
        if (background != null) {
            background.getPadding(this.ˋˋ);
            this.ˏॱ = (this.ˋˋ.left + this.ˋˋ.right) + i;
            return;
        }
        ॱॱ(i);
    }

    public void ˊ(@Nullable OnItemClickListener onItemClickListener) {
        this.ˊˋ = onItemClickListener;
    }

    public void ˊ() {
        boolean z = true;
        boolean z2 = false;
        int i = -1;
        int ˏ = ˏ();
        boolean ͺ = ͺ();
        iw.ˎ(this.ʼ, this.ॱˊ);
        int i2;
        PopupWindow popupWindow;
        if (!this.ʼ.isShowing()) {
            if (this.ˏॱ == -1) {
                i2 = -1;
            } else if (this.ˏॱ == -2) {
                i2 = ʻ().getWidth();
            } else {
                i2 = this.ˏॱ;
            }
            if (this.ͺ == -1) {
                ˏ = -1;
            } else if (this.ͺ != -2) {
                ˏ = this.ͺ;
            }
            this.ʼ.setWidth(i2);
            this.ʼ.setHeight(ˏ);
            ˋ(true);
            popupWindow = this.ʼ;
            if (this.ʾ || this.ʽॱ) {
                z = false;
            }
            popupWindow.setOutsideTouchable(z);
            this.ʼ.setTouchInterceptor(this.ˉ);
            if (this.ॱᐝ) {
                iw.ˎ(this.ʼ, this.ʻॱ);
            }
            if (ᐝ != null) {
                try {
                    ᐝ.invoke(this.ʼ, new Object[]{this.ˌ});
                } catch (Throwable e) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e);
                }
            }
            iw.ˋ(this.ʼ, ʻ(), this.ˊॱ, this.ˋॱ, this.ॱˎ);
            this.ˋ.setSelection(-1);
            if (!this.ˏˎ || this.ˋ.isInTouchMode()) {
                ˊॱ();
            }
            if (!this.ˏˎ) {
                this.ʻ.post(this.ˋᐝ);
            }
        } else if (ViewCompat.ˋˊ(ʻ())) {
            int i3;
            int i4;
            if (this.ˏॱ == -1) {
                i3 = -1;
            } else if (this.ˏॱ == -2) {
                i3 = ʻ().getWidth();
            } else {
                i3 = this.ˏॱ;
            }
            if (this.ͺ == -1) {
                if (!ͺ) {
                    ˏ = -1;
                }
                PopupWindow popupWindow2;
                if (ͺ) {
                    popupWindow2 = this.ʼ;
                    if (this.ˏॱ == -1) {
                        i2 = -1;
                    } else {
                        i2 = 0;
                    }
                    popupWindow2.setWidth(i2);
                    this.ʼ.setHeight(0);
                    i4 = ˏ;
                } else {
                    popupWindow2 = this.ʼ;
                    if (this.ˏॱ == -1) {
                        i2 = -1;
                    } else {
                        i2 = 0;
                    }
                    popupWindow2.setWidth(i2);
                    this.ʼ.setHeight(-1);
                    i4 = ˏ;
                }
            } else if (this.ͺ == -2) {
                i4 = ˏ;
            } else {
                i4 = this.ͺ;
            }
            popupWindow = this.ʼ;
            if (!(this.ʾ || this.ʽॱ)) {
                z2 = true;
            }
            popupWindow.setOutsideTouchable(z2);
            popupWindow = this.ʼ;
            View ʻ = ʻ();
            ˏ = this.ˊॱ;
            int i5 = this.ˋॱ;
            if (i3 < 0) {
                i3 = -1;
            }
            if (i4 >= 0) {
                i = i4;
            }
            popupWindow.update(ʻ, ˏ, i5, i3, i);
        }
    }

    public void ˋ() {
        this.ʼ.dismiss();
        ॱ();
        this.ʼ.setContentView(null);
        this.ˋ = null;
        this.ʻ.removeCallbacks(this.ॱ);
    }

    public void ॱ(@Nullable OnDismissListener onDismissListener) {
        this.ʼ.setOnDismissListener(onDismissListener);
    }

    private void ॱ() {
        if (this.ˈ != null) {
            ViewParent parent = this.ˈ.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.ˈ);
            }
        }
    }

    public void ʻ(int i) {
        this.ʼ.setInputMethodMode(i);
    }

    public void ᐝ(int i) {
        ch chVar = this.ˋ;
        if (ˎ() && chVar != null) {
            chVar.ˊ(false);
            chVar.setSelection(i);
            if (chVar.getChoiceMode() != 0) {
                chVar.setItemChecked(i, true);
            }
        }
    }

    public void ˊॱ() {
        ch chVar = this.ˋ;
        if (chVar != null) {
            chVar.ˊ(true);
            chVar.requestLayout();
        }
    }

    public boolean ˎ() {
        return this.ʼ.isShowing();
    }

    public boolean ͺ() {
        return this.ʼ.getInputMethodMode() == 2;
    }

    @Nullable
    public ListView ᐝ() {
        return this.ˋ;
    }

    @NonNull
    protected ch ˏ(Context context, boolean z) {
        return new ch(context, z);
    }

    private int ˏ() {
        int i;
        int i2;
        int i3;
        int i4;
        boolean z = true;
        LayoutParams layoutParams;
        View view;
        if (this.ˋ == null) {
            Context context = this.ॱॱ;
            this.ˍ = new Runnable(this) {
                final /* synthetic */ ListPopupWindow ˏ;

                {
                    this.ˏ = r1;
                }

                public void run() {
                    View ʻ = this.ˏ.ʻ();
                    if (ʻ != null && ʻ.getWindowToken() != null) {
                        this.ˏ.ˊ();
                    }
                }
            };
            this.ˋ = ˏ(context, !this.ˏˎ);
            if (this.ˊᐝ != null) {
                this.ˋ.setSelector(this.ˊᐝ);
            }
            this.ˋ.setAdapter(this.ʽ);
            this.ˋ.setOnItemClickListener(this.ˊˋ);
            this.ˋ.setFocusable(true);
            this.ˋ.setFocusableInTouchMode(true);
            this.ˋ.setOnItemSelectedListener(new OnItemSelectedListener(this) {
                final /* synthetic */ ListPopupWindow ॱ;

                {
                    this.ॱ = r1;
                }

                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
                    if (i != -1) {
                        ch chVar = this.ॱ.ˋ;
                        if (chVar != null) {
                            chVar.ˊ(false);
                        }
                    }
                }

                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });
            this.ˋ.setOnScrollListener(this.ˎˎ);
            if (this.ˊˊ != null) {
                this.ˋ.setOnItemSelectedListener(this.ˊˊ);
            }
            View view2 = this.ˋ;
            View view3 = this.ˈ;
            if (view3 != null) {
                View linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                ViewGroup.LayoutParams layoutParams2 = new LayoutParams(-1, 0, 1.0f);
                switch (this.ʼॱ) {
                    case 0:
                        linearLayout.addView(view3);
                        linearLayout.addView(view2, layoutParams2);
                        break;
                    case 1:
                        linearLayout.addView(view2, layoutParams2);
                        linearLayout.addView(view3);
                        break;
                    default:
                        Log.e("ListPopupWindow", "Invalid hint position " + this.ʼॱ);
                        break;
                }
                if (this.ˏॱ >= 0) {
                    i = this.ˏॱ;
                    i2 = Integer.MIN_VALUE;
                } else {
                    i2 = 0;
                    i = 0;
                }
                view3.measure(MeasureSpec.makeMeasureSpec(i, i2), 0);
                layoutParams = (LayoutParams) view3.getLayoutParams();
                i2 = layoutParams.bottomMargin + (view3.getMeasuredHeight() + layoutParams.topMargin);
                view = linearLayout;
            } else {
                view = view2;
                i2 = 0;
            }
            this.ʼ.setContentView(view);
            i3 = i2;
        } else {
            ViewGroup viewGroup = (ViewGroup) this.ʼ.getContentView();
            view = this.ˈ;
            if (view != null) {
                layoutParams = (LayoutParams) view.getLayoutParams();
                i3 = layoutParams.bottomMargin + (view.getMeasuredHeight() + layoutParams.topMargin);
            } else {
                i3 = 0;
            }
        }
        Drawable background = this.ʼ.getBackground();
        if (background != null) {
            background.getPadding(this.ˋˋ);
            i2 = this.ˋˋ.top + this.ˋˋ.bottom;
            if (this.ᐝॱ) {
                i4 = i2;
            } else {
                this.ˋॱ = -this.ˋˋ.top;
                i4 = i2;
            }
        } else {
            this.ˋˋ.setEmpty();
            i4 = 0;
        }
        if (this.ʼ.getInputMethodMode() != 2) {
            z = false;
        }
        i = ˊ(ʻ(), this.ˋॱ, z);
        if (this.ʽॱ || this.ͺ == -1) {
            return i + i4;
        }
        int makeMeasureSpec;
        switch (this.ˏॱ) {
            case -2:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ॱॱ.getResources().getDisplayMetrics().widthPixels - (this.ˋˋ.left + this.ˋˋ.right), Integer.MIN_VALUE);
                break;
            case -1:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ॱॱ.getResources().getDisplayMetrics().widthPixels - (this.ˋˋ.left + this.ˋˋ.right), 1073741824);
                break;
            default:
                makeMeasureSpec = MeasureSpec.makeMeasureSpec(this.ˏॱ, 1073741824);
                break;
        }
        i2 = this.ˋ.ˏ(makeMeasureSpec, 0, -1, i - i3, -1);
        if (i2 > 0) {
            i3 += (this.ˋ.getPaddingTop() + this.ˋ.getPaddingBottom()) + i4;
        }
        return i2 + i3;
    }

    @RestrictTo({androidx.annotation.RestrictTo.d.LIBRARY_GROUP})
    public void ˏ(boolean z) {
        this.ॱᐝ = true;
        this.ʻॱ = z;
    }

    private void ˋ(boolean z) {
        if (ˏ != null) {
            try {
                ˏ.invoke(this.ʼ, new Object[]{Boolean.valueOf(z)});
            } catch (Exception e) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    private int ˊ(View view, int i, boolean z) {
        if (ˊ != null) {
            try {
                return ((Integer) ˊ.invoke(this.ʼ, new Object[]{view, Integer.valueOf(i), Boolean.valueOf(z)})).intValue();
            } catch (Exception e) {
                Log.i("ListPopupWindow", "Could not call getMaxAvailableHeightMethod(View, int, boolean) on PopupWindow. Using the public version.");
            }
        }
        return this.ʼ.getMaxAvailableHeight(view, i);
    }
}
